﻿using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
using System;
using System.Text;
using System.Linq;
namespace MetMax
{
    public partial class MetMaxDataSet {
        partial class result_LibrarySearchDataTable
        {
            public MetMaxDataSet.result_LibrarySearchRow Addresult_LibrarySearchRow(string spectrum, string analyte)
            {
                result_LibrarySearchRow newrow = ((result_LibrarySearchRow)(this.NewRow()));
                newrow.spectrum = spectrum;
                newrow.analyte_name = analyte;
                this.Rows.Add(newrow);
                return newrow;
            }

            public MetMaxDataSet.result_LibrarySearchDataTable finish()
            {
                //fill the column "row load"
                MetMaxDataSet.computeRowLoad(this, getResultColumns(), this.Columns[row_loadColumn.ColumnName], MasterColumnCount());

                //count the columns load
                MetMaxDataSet.computeColumnLoad(this, getResultColumns());

                AcceptChanges();

                return this;
            }

            protected IEnumerable<DataColumn> getResultColumns()
            {
                foreach (DataColumn col in this.Columns)
                {
                    if (!isMasterColumn(col))
                    {
                        yield return col;
                    }
                }
                yield break;
            }

            public int MasterColumnCount()
            {
                return 3;
            }

            public bool isMasterColumn(DataColumn col)
            {
                using (DataTable dt = new result_LibrarySearchDataTable())
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.ColumnName.Equals(col.ColumnName))
                        {
                            return true;
                        }
                    }
                    return false;
                }
            }

            public IEnumerable<string> ResultColumnNames()
            {
                foreach (DataColumn tmpColumn in this.Columns)
                {
                    if (!isMasterColumn(tmpColumn))
                    {
                        yield return tmpColumn.ColumnName;
                    }
                }
                yield break;
            }

        }

        partial class resultDataTable
        {
            public MetMaxDataSet.resultRow AddresultDataTableRow(string name, string mass, float ri) {
                resultRow rowresultDataTableRow = ((resultRow)(this.NewRow()));
                rowresultDataTableRow.name = name;
                rowresultDataTableRow.mass = mass;
                rowresultDataTableRow.ri = ri;
                this.Rows.Add(rowresultDataTableRow);
                return rowresultDataTableRow;
            }

            public MetMaxDataSet.resultDataTable finish()
            {
                //fill the column "row load"
                MetMaxDataSet.computeRowLoad(this, getResultColumns(), this.Columns[row_loadColumn.ColumnName], MasterColumnCount());

                //count the columns load
                MetMaxDataSet.computeColumnLoad(this, getResultColumns());

                ////round the computed column
                //roundColumn(this.Columns[DataTable_v2.riColumneName], 2);
                //roundColumn(this.Columns[DataTable_v2.rowloadColumneName], 2);
                //roundColumn(this.Columns[DataTable_v2.rt1ColumneName], 2);
                //roundColumn(this.Columns[DataTable_v2.rt2ColumneName], 2);
                //roundColumn(this.Columns[DataTable_v2.ri1poly3ColumneName], 2);
                //roundColumn(this.Columns[DataTable_v2.ri1vandendoolColumneName], 2);
                //roundColumn(this.Columns[DataTable_v2.ri1kovacColumneName], 2);
                //roundColumn(this.Columns[DataTable_v2.ri2vandendoolColumneName], 2);

                AcceptChanges();

                return this;
            }


            public void roundColumn(DataColumn col, int decimals)
            {
                //Parallel.ForEach<DataRow>(this.Rows, delegate(DataRow tmpRow)
                //{
                //    if (tmpRow[col] != DBNull.Value)
                //    {
                //        tmpRow[col] = Math.Round((float)tmpRow[col], decimals);
                //    }
                //});
                foreach (DataRow tmpRow in this.Rows)
                {
                    if (tmpRow[col] != DBNull.Value)
                    {
                        tmpRow[col] = Math.Round((float)tmpRow[col], decimals);
                    }
                }
            }

            protected IEnumerable<DataColumn> getResultColumns()
            {
                foreach (DataColumn col in this.Columns)
                {
                    if (!isMasterColumn(col))
                    {
                        yield return col;
                    }
                }
                yield break;
            }

            public int MasterColumnCount()
            {
                return 4;
            }

            public bool isMasterColumn(DataColumn col)
            {
                using (DataTable dt = new resultDataTable())
                {
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.ColumnName.Equals(col.ColumnName))
                        {
                            return true;
                        }
                    }
                    return false;
                }
            }

            public IEnumerable<string> ResultColumnNames()
            {
                foreach (DataColumn tmpColumn in this.Columns)
                {
                    if (!isMasterColumn(tmpColumn))
                    {
                        yield return tmpColumn.ColumnName;
                    }
                }
                yield break;
            }

        }


        private static void computeRowLoad(DataTable dt, IEnumerable<DataColumn> resultColumns, DataColumn rowloadColumn, int MasterColumnCount)
        {
            foreach (DataRow dr in dt.Rows)
            {
                dr[rowloadColumn] = (double)dr.ItemArray.OfType<BusinessObject.Base>().Count() / (double)(dt.Columns.Count - MasterColumnCount);
            }
        }

        private static void computeColumnLoad(DataTable dt, IEnumerable<DataColumn> resultColumns)
        {
            foreach (DataColumn tmpColumn in resultColumns)
            {
                int columnLoad = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr[tmpColumn] != System.DBNull.Value)
                    {
                        columnLoad++;
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(tmpColumn.ColumnName);
                sb.AppendLine(string.Format("column load: {0}", Math.Round((double)columnLoad / (double)dt.Rows.Count, 2)));
                tmpColumn.ColumnName = sb.ToString();
            }
        }



    }
}
